package com.ka.magicexplorer.fragment;

import java.util.List;

import com.ka.magicexplorer.activity.R;
import com.ka.magicexplorer.adapter.AdapterGallery;
import com.ka.magicexplorer.model.FileSuper;
import com.ka.magicexplorer.provider.MySession;

import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class FragmentGallery extends BasicFragment {

	protected AdapterGallery adg;
	protected List<FileSuper> lstData;
	protected String str;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_gallery, null);
	}

	protected void init() {
		logoDocument = (ImageView) getActivity().findViewById(R.id.goto_menu);
		textTitle = (TextView) getActivity().findViewById(R.id.content_title);
		editText = (EditText) getActivity().findViewById(R.id.inputText);
		search_button = (ImageView) getActivity().findViewById(
				R.id.search_button);
		gridViewContent = (GridView) getActivity().findViewById(
				R.id.gallery_view_content);
		lstData = MySession.getInstance().getPngFileList();

	}

	protected void execute() {
		
		rotateChange();
		logoDocument.setBackgroundResource(R.drawable.galery_logo);
		textTitle.setText(getResources().getString(R.string.gallery));

		MySession.MenuState state = MySession.getInstance().getCurrentChildFocus();
		
		if (!state.equals(MySession.MenuState.noState)) {
			if (state.equals(MySession.MenuState.pngState)) {
				lstData = MySession.getInstance().getPngFileList();
			} else {
				if (state.equals(MySession.MenuState.jpgState)) {
					lstData = MySession.getInstance().getJpgFileList();
				} else {
					lstData = MySession.getInstance().getGifFileList();
				}
			}
		} else {

			lstData = MySession.getInstance().getPngFileList();
			lstData.addAll(MySession.getInstance().getJpgFileList());
			lstData.addAll(MySession.getInstance().getGifFileList());
		}

		adg = new AdapterGallery(getActivity(), lstData);
		gridViewContent.setAdapter(adg);

	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		
		rotateChange();
	}

	private void rotateChange() {
		if( getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
			gridViewContent.setNumColumns(5);
		}else
		{
			gridViewContent.setNumColumns(4);
		}
	}
	
	@Override
    public void onResume() {
        super.onResume();
        adg.actionResume();
        adg.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        adg.actionPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        adg.actionDestroy();
    }
}
